package com.wulian.controller;

import com.wulian.entities.*;
import com.wulian.result.CodeStatus;
import com.wulian.result.ResponseResult;
import com.wulian.service.DoctorService;
import com.wulian.service.RecordService;
import com.wulian.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "挂号操作接口")
@RestController
@RequestMapping(value = "/register")
public class RegisterController {

    @Resource
    RegisterService registerService;

    @Resource
    RecordService recordService;

    @Resource
    DoctorService doctorService;

    @ApiOperation("挂号")
    @PostMapping(value = "/")
    public ResponseResult<Object> patientRegister(@RequestBody Register register) {
        try {
            registerService.addRegister(register);
            return new ResponseResult<>(CodeStatus.OK, "挂号成功");
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }

    public void selRegister(int id) {
        Register register = registerService.selRegister(id);
        Record record = new Record();
        Patient patient = new Patient();
        patient.setPatient_id(register.getReg_id());//挂号id为病人id
        patient.setPatient_name(register.getReg_name());//名字
        patient.setPatient_id_card(register.getReg_id_card());//身份证
        record.setPatient(patient);//添加病人

        Doctor doctor = doctorService.selDocById(register.getReg_doctor().getDoctor_id());
        record.setDoctor(doctor);//添加医生
        recordService.addRecord(record);
    }


    @ApiOperation("更改挂号状态（更改后写入病历相关信息表内）")
    @PutMapping(value = "/{id}")
    public ResponseResult<Object> updateRegister(@PathVariable int id) {
        try {
            registerService.updateRegister(id);
            selRegister(id);
            return new ResponseResult<>(CodeStatus.OK, "状态更改成功");
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }
    @ApiOperation("未就诊名单根据医生id")
    @GetMapping(value = "/{doc_id}")
    public ResponseResult<List<Register>> selAllRegister(@PathVariable int doc_id) {
        try {
            List<Register> registerList = registerService.selAllRegister(doc_id);

            return new ResponseResult<>(CodeStatus.OK, "查询成功",registerList);
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }
    @ApiOperation("分页查询挂号信息")
    @GetMapping("/{pageNum}/{size}")
    public ResponseResult<List<Register>> getRegistersByPage(@PathVariable Integer pageNum,@PathVariable Integer size){
        if(pageNum<=0&&size<=0){
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST,"参数错误");
        }
        List<Register> registerList = registerService.getAllByPage(pageNum, size);
        try {
            if (registerList==null){
                return  new ResponseResult<>(CodeStatus.NOT_FOUND, "未查询到相关数据");
            }
            else {
                return new ResponseResult<>(CodeStatus.OK,"查询成功",registerList);
            }
        }catch (Exception e){
            return new ResponseResult<>(CodeStatus.FAIL,e);
        }
    }
    @ApiOperation("删除所有挂号记录（每天自动,假删除）")
    @PutMapping(value = "/")
    public ResponseResult<Object> delAllRegister() {
        try {
            registerService.delAllRegister();
            return new ResponseResult<>(CodeStatus.OK, "删除成功");
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }
}
